package cn.swjtuhc.ss.hljapi.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.swjtuhc.ss.hljapi.exception.BusinessException;
import cn.swjtuhc.ss.hljapi.pojo.User;
import cn.swjtuhc.ss.hljapi.pojo.VolunteerLoginRequest;
import cn.swjtuhc.ss.hljapi.pojo.response.VolunteerLoginResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.swjtuhc.ss.hljapi.pojo.Volunteer;
import cn.swjtuhc.ss.hljapi.service.VolunteerService;
import cn.swjtuhc.ss.hljapi.mapper.VolunteerMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 15854
* @description 针对表【volunteer】的数据库操作Service实现
* @createDate 2024-08-27 19:00:40
*/
@Service
public class VolunteerServiceImpl extends ServiceImpl<VolunteerMapper, Volunteer>
    implements VolunteerService{
    @Autowired
    VolunteerMapper volunteerMapper;
    @Override
    public void register(Volunteer volunteer) {
        if (this.checkUsernameExists(volunteer.getUsername())) {
            throw new BusinessException("用户名已经存在");
        }
        volunteerMapper.insert(volunteer);
    }

    @Override
    public Boolean checkUsernameExists(String username) {
        return lambdaQuery().eq(Volunteer::getUsername, username).exists();
    }

    @Override
    public VolunteerLoginResponse login(VolunteerLoginRequest request) {
        LambdaQueryWrapper<Volunteer> wrapper = new LambdaQueryWrapper<Volunteer>();
        wrapper.eq(Volunteer::getUsername, request.getUsername());
        Volunteer volunteer = getOne(wrapper);
        if(volunteer == null || !volunteer.getPassword().equals(request.getPassword())) {
            throw new BusinessException("账号或密码错误");
        }
        return BeanUtil.copyProperties(volunteer, VolunteerLoginResponse.class);
    }
}




